package com.order.controller.seller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.order.mapper.ProductInfoMapper;
import com.order.pojo.ProductCategory;
import com.order.pojo.ProductInfo;
import com.order.service.IProductInfoService;
import com.order.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

@Api(tags = "商品信息接口")
@RestController
@RequestMapping("/seller/product-info")
public class ProductInfoController {

    @Autowired
    IProductInfoService productInfoService;

    @Autowired
    RedisTemplate redisTemplate;

    @ApiOperation(value = "获取所有商品信息")
    @GetMapping("/")
    public Result getAllProductInfo() {
        List<ProductInfo> productInfos = productInfoService.list(new QueryWrapper<ProductInfo>().eq("product_del",0));
        if (productInfos == null || productInfos.isEmpty()) {
            return Result.error("没有商品信息");
        }
        return Result.success(productInfos);
    }

    @ApiOperation(value = "根据ID获取商品信息")
    @GetMapping("{productId}")
    public Result getProductInfoById(@PathVariable("productId") String productId) {
        ProductInfo productInfo = productInfoService.getById(productId);
        if (productInfo == null) {
            return Result.error("商品信息不存在");
        }
        return Result.success(productInfo);
    }

    @ApiOperation(value = "添加商品信息")
    @PostMapping("/add")
    public Result addProductInfo(@RequestBody ProductInfo productInfo) {
        boolean flag = redisTemplate.delete("productVOList");
        if (!flag) {
            return Result.error("缓存清除失败，请稍后再试");
        }
        String id = String.valueOf(IdUtil.getSnowflake().nextId());
        productInfo.setProductId(id);
        if(productInfoService.save(productInfo)) {
            return Result.success("商品信息添加成功");
        } else {
            return Result.error("商品信息添加失败");
        }
    }

    @ApiOperation(value = "更新商品信息")
    @PutMapping("/update")
    public Result updateProductInfo(@RequestBody ProductInfo productInfo) {
        boolean flag = redisTemplate.delete("productVOList");
        if (!flag) {
            return Result.error("缓存清除失败，请稍后再试");
        }
        productInfo.setUpdateTime(DateUtil.now());
        if(productInfoService.updateById(productInfo)) {
            return Result.success("商品信息更新成功");
        } else {
            return Result.error("商品信息更新失败");
        }
    }

    @ApiOperation(value = "删除商品信息")
    @DeleteMapping("/delete/{productId}")
    public Result deleteProductInfo(@PathVariable("productId") String productId) {
        boolean flag = redisTemplate.delete("productVOList");
        if (!flag) {
            return Result.error("缓存清除失败，请稍后再试");
        }
        ProductInfo productInfo = new ProductInfo(productId,1);
        if(productInfoService.updateById(productInfo)){
            return Result.success("商品信息删除成功");
        } else {
            return Result.error("商品信息删除失败");
        }
    }

    @ApiOperation(value = "根据页面获取所有商品信息")
    @GetMapping("/page")
    public Result getProductCategoryByPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer page,
                                           @RequestParam(value = "pageSize", defaultValue = "8") Integer size,
                                           ProductInfo productInfo) {
        PageInfo<ProductInfo> pageInfo = productInfoService.getCategoriesByPage(page, size, productInfo);
        return Result.success(pageInfo);
    }
}
